﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tesla.Service.WcfServiceCommon;
using System.ServiceModel;
using Tesla.Service.ServiceContracts;
using Tesla.Service.DataContracts;
using Tesla.Util.ValidateEntity;


namespace Tesla.Web
{
    public class UserLoginServiceDispatcher
    {
        public void ValidateUser(string userName, string password, ref bool? userExists)
        {
            ISvcUserLogin _proxySvcUserLogin;
            ChannelFactory<ISvcUserLogin> chfUserLogin = null;

            try
            {
                chfUserLogin = GADNChannelFactoryProvider.CreateChannelFactory<ISvcUserLogin>();
                _proxySvcUserLogin = chfUserLogin.CreateChannel();
                _proxySvcUserLogin.ValidateUser(userName, password, ref userExists);
            }
            catch (FaultException gfx)
            {
                if (chfUserLogin.State == CommunicationState.Faulted || chfUserLogin.State == CommunicationState.Opened)
                    chfUserLogin.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfUserLogin.State == CommunicationState.Faulted || chfUserLogin.State == CommunicationState.Opened)
                    chfUserLogin.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfUserLogin.Close();
            }
        }
    }
}

